package com.situ.blog.controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/page/*")
public class pageController extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String servletPath = req.getServletPath();
        //  /page
        System.out.println("servletPath:" + servletPath);
        // /page/blog
        // /page/blog/list  /page/blog/add   /page/blog/update
        String requestURI = req.getRequestURI();
        System.out.println("requestURI: " + requestURI);
        String[] paths = requestURI.split("/");
        //["","page","blog","list"]
        //String pageURI = requestURI.substring(requestURI.lastIndexOf("/"));
        //req.getRequestDispatcher("/WEB-INF/page/" + pageURI + ".jsp").forward(req,resp);
        //req.getRequestDispatcher("/WEB-INF/page/" + paths[2] +"_"+paths[3]+ ".jsp").forward(req,resp);
        if (paths.length == 4){
            req.getRequestDispatcher("/WEB-INF/page/" + paths[2] + "_" + paths[3] + ".jsp").forward(req,resp);
        } else if (paths.length == 3){ // 登录界面：/page/login 只有两级
            req.getRequestDispatcher("/WEB-INF/page/" + paths[2] + ".jsp").forward(req,resp);
        }

    }
}
